//
//  LHXButton.m
//  MyButtonDemo
//
//  Created by lemo on 2017/11/20.
//  Copyright © 2017年 lemo. All rights reserved.
//

#import "LHXButton.h"

@implementation LHXButton

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGRect titleLabelFrame = self.titleLabel.frame;
    CGRect imageViewFrame = self.imageView.frame;
    switch (self.btnType) {
        case LHXButtonImageTypeOnLeft:
        {
            CGFloat imageX = (self.frame.size.width - imageViewFrame.size.width - titleLabelFrame.size.width - self.spacing) * 0.5;
            imageViewFrame.origin.x = imageX;
            self.imageView.frame = imageViewFrame;
            titleLabelFrame.origin.x = CGRectGetMaxX(imageViewFrame) + self.spacing;
            self.titleLabel.frame = titleLabelFrame;
        }
            break;
        case LHXButtonImageTypeOnRight:
        {
            CGFloat titleLabelX = (self.frame.size.width - imageViewFrame.size.width - titleLabelFrame.size.width - self.spacing) * 0.5;
            titleLabelFrame.origin.x = titleLabelX;
            self.titleLabel.frame = titleLabelFrame;
            imageViewFrame.origin.x = CGRectGetMaxX(titleLabelFrame) + self.spacing;
            self.imageView.frame = imageViewFrame;
        }
            break;
        case LHXButtonImageTypeOnAbove:
        {
            CGFloat imageX = (self.frame.size.width - imageViewFrame.size.width) * 0.5;
            CGFloat imageY = (self.frame.size.height - imageViewFrame.size.height - titleLabelFrame.size.height - self.spacing) * 0.5;
            imageViewFrame.origin.x = imageX;
            imageViewFrame.origin.y = imageY;
            self.imageView.frame = imageViewFrame;
            
            titleLabelFrame.origin.y = CGRectGetMaxY(imageViewFrame) + self.spacing;
            titleLabelFrame.size.width = self.frame.size.width;
            titleLabelFrame.origin.x = 0;
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
            self.titleLabel.frame = titleLabelFrame;
        }
            break;
        case LHXButtonImageTypeOnBelow:
        {
            titleLabelFrame.origin.y = (self.frame.size.height - imageViewFrame.size.height - titleLabelFrame.size.height - self.spacing) * 0.5;
            titleLabelFrame.size.width = self.frame.size.width;
            titleLabelFrame.origin.x = 0;
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
            self.titleLabel.frame = titleLabelFrame;
            
            CGFloat imageX = (self.frame.size.width - imageViewFrame.size.width) * 0.5;
            CGFloat imageY = CGRectGetMaxY(titleLabelFrame) + self.spacing;
            imageViewFrame.origin.x = imageX;
            imageViewFrame.origin.y = imageY;
            self.imageView.frame = imageViewFrame;
            
        }
            break;
            
        default:
            break;
    }
}

@end
